App.jsx ➔ checkloggedIn   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
1
import React, { useState, useEffect } from "react";
2
import { Route, Routes } from "react-router-dom";
3
import { Footer, Header, LoginForm } from "./components";
4
import auth from "./models/auth.js";
5
import { Home, LoginFailure, LoginSuccess, Logout, Account, About } from "./pages";
6
import { useStateContext } from "./contexts/ContextProvider";
7
const App = () => {
8
  const [displayForm, setDisplayForm] = useState(false);
9
  const {
10
    isLoggedIn,
11
    setIsLoggedIn,
12
    isGoogleAcc,
13
    setIsGoogleAcc,
14
    setIsServerAcc,
15
    isServerAcc,
16
  } = useStateContext();
17
18
  useEffect(() => {
19
    async function checkloggedIn() {
20
      const res = auth.loggedIn("JWT");
21
      console.log("------------- APP.JSX -------------");
22
      if (res) {
23
        setIsServerAcc(true);
24
        setIsLoggedIn(true);
25
      }
26
      console.log("-----------------------------------");
27
    }
28
    checkloggedIn();
29
  }, []);
30
31
  useEffect(() => {
32
    async function checkloggedIn() {
33
      const resGoogle = await auth.getUser();
34
      if (resGoogle) {
35
        console.log("resGoogle Ska SÄTTAS TILL TRUE");
36
        setIsGoogleAcc(true);
37
        setIsLoggedIn(true);
38
      }
39
      console.log("isGoogleAcc", isGoogleAcc);
40
    }
41
    checkloggedIn();
42
  }, []);
43
44
  const overlay = () => {
45
    let state = { click: "auto", backdrop: "blur(0px)" };
46
    document.body.style.overflow = "visible";
47
    if (displayForm) {
48
      document.body.style.overflow = "hidden";
49
      state = { click: "none", backdrop: "blur(4px)" };
50
    }
51
    return state;
52
  };
53
54
  return (
55
    <div>
56
      {displayForm ? (
57
        <div
58
          className="fixed top-1/2 left-1/2 z-10
59
        transform -translate-x-1/2 -translate-y-1/2"
60
        >
61
          <LoginForm setDisplayForm={setDisplayForm} />
62
        </div>
63
      ) : null}
64
      <div
65
        className="flex flex-col h-full min-h-screen w-full justify-between"
66
        style={{
67
          pointerEvents: overlay().click,
68
          filter: overlay().backdrop,
69
        }}
70
      >
71
        <Header setDisplayForm={setDisplayForm} />
72
73
        <div>
74
          <Routes>
75
            <Route path="/" element={<Home />} />
76
            <Route path="/login/google/failure" element={<LoginFailure />} />
77
            <Route path="/login/google/success" element={<LoginSuccess />} />
78
            <Route path="/logout/google" element={<Logout />} />
79
            <Route path="/about" element={<About />} />
80
            {isGoogleAcc || isServerAcc ? (
81
              <Route path="/account" element={<Account />} />
82
            ) : null}
83
          </Routes>
84
        </div>
85
        <Footer />
86
      </div>
87
    </div>
88
  );
89
};
90
91
export default App;
92